#include "UIFunction.h"

#include <cstdio>
#include <iostream>
#include <WidgetsManager.h>
#include <WidgetLog.h>


static const char *level_strings_ui[] = {
    "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
};

void UIFun::test(const QString& info)
{
    std::cout<<"UIFun::test"<<"\n";
}

void UIFun::callback_ui_log(log_Event *ev)
{
    //TODO: For some EString, the print result in console is right
    //but for ui log widget, print result is wrong, for example EConrollerQtTree refreshed and 
    //log last EItem'name and depth is wrong. This is only happend in linux.
    WidgetLog* wLog = dynamic_cast<WidgetLog*>(WidgetsManager::instance()->getWidgetFromMap("WidgetLog"));
    if(wLog == nullptr)
        return;

    char buf[16];
    buf[strftime(buf, sizeof(buf), "%H:%M:%S", ev->time)] = '\0';

    char strPre[300];
    sprintf(strPre, "%s %-5s %s:%d: ", 
            buf, level_strings_ui[ev->level], ev->file, ev->line);

    wLog->addLogText(strPre);
    char strContent[1024];
    vsprintf(strContent, ev->fmt, ev->ap);

    
    wLog->addLogText(QString::fromUtf8(strContent));

    /**TODO: Above code will show as two line strings in WidgetLog.
     * So, how to convert to one line?*/
}

